/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.auth.endpoint;

import cn.hutool.http.HttpRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springblade.auth.utils.CasUtil;
import org.springblade.core.jwt.JwtUtil;
import org.springblade.core.jwt.props.JwtProperties;
import org.springblade.core.launch.constant.TokenConstant;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.social.props.SocialProperties;
import org.springblade.core.social.utils.SocialUtil;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.WebUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * SocialEndpoint
 *
 * @author Chill
 */
@NonDS
@Slf4j
@RestController
@AllArgsConstructor
public class CasEndPoint {
	@Resource
	private CasUtil casUtil;
	public static final String COOKIE_USER_TICKET = "cookie_user_ticket";
	private final JwtProperties jwtProperties;

	/**
	 * 撤销授权
	 */
	@GetMapping("/cas/logout")
	public Object revokeAuth(String tenantId, String userId,String token) {
		if (userId != null && jwtProperties.getState()) {
			JwtUtil.removeAccessToken(tenantId, userId, token);
		}
		return "success";
	}

	/**
	 * 设置token
	 */
	@GetMapping("/cas/setToken")
	public Object setToken(String token) {
		log.info("cas客户端调用服务端开始token:{}",token);
		String userTicket = WebUtil.getCookieVal(COOKIE_USER_TICKET);
		log.info("获取的userTicket:{}",userTicket);
		boolean flag = casUtil.sendMessage(userTicket,token);
		log.info("cas客户端调用服务端结束:{}",flag);
		return flag;
	}

}
